<template>
  <div class="color-picker">
    <el-popover placement="bottom" :width="280" trigger="click" popper-class="color-picker-popover">
      <template #reference>
        <el-button
          circle
          class="color-picker-trigger"
          :style="{ backgroundColor: currentColor.primary }"
          :title="'当前主题色：' + currentColor.name"
        >
          <el-icon><brush /></el-icon>
        </el-button>
      </template>

      <div class="color-picker-content">
        <div class="color-picker-title">选择主题颜色</div>
        <div class="color-grid">
          <div
            v-for="(color, key) in THEME_COLORS"
            :key="key"
            class="color-item"
            :class="{ active: currentThemeColor === key }"
            :style="{ backgroundColor: color.primary }"
            :title="color.name"
            @click="handleColorChange(key)"
          >
            <el-icon v-if="currentThemeColor === key" class="check-icon">
              <check />
            </el-icon>
          </div>
        </div>

        <div class="color-preview">
          <div class="preview-title">预览效果</div>
          <div class="preview-buttons">
            <el-button type="primary" size="small"> 主要按钮 </el-button>
            <el-button type="primary" plain size="small"> 次要按钮 </el-button>
            <el-button type="primary" text size="small"> 文字按钮 </el-button>
          </div>
        </div>
      </div>
    </el-popover>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue';
import { Brush, Check } from '@element-plus/icons-vue';
import { useThemeStore, THEME_COLORS } from '@/stores/theme';

const themeStore = useThemeStore();

const currentThemeColor = computed(() => themeStore.currentThemeColor);
const currentColor = computed(() => THEME_COLORS[currentThemeColor.value]);

const handleColorChange = (colorKey: keyof typeof THEME_COLORS) => {
  themeStore.setThemeColor(colorKey);
};
</script>

<style scoped>
.color-picker-trigger {
  position: relative;
  border: 2px solid var(--el-border-color);
  transition: all 0.3s ease;
}

.color-picker-trigger:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 12px rgb(0 0 0 / 15%);
}

.color-picker-trigger .el-icon {
  text-shadow: 0 1px 2px rgb(0 0 0 / 30%);
}
</style>

<style>
.color-picker-popover {
  padding: 16px !important;
}

.color-picker-content {
  user-select: none;
}

.color-picker-title {
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 12px;
  text-align: center;
}

.color-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.color-item {
  width: 60px;
  height: 40px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid transparent;
  position: relative;
}

.color-item:hover {
  transform: scale(1.05);
  box-shadow: 0 2px 8px rgb(0 0 0 / 15%);
}

.color-item.active {
  border-color: var(--el-color-white);
  box-shadow: 0 0 0 2px var(--el-color-primary);
}

.check-icon {
  font-size: 16px;
  text-shadow: 0 1px 2px rgb(0 0 0 / 30%);
}

.color-preview {
  border-top: 1px solid var(--el-border-color-lighter);
  padding-top: 12px;
}

.preview-title {
  font-size: 12px;
  margin-bottom: 8px;
}

.preview-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}
</style>
